import requests
from bs4 import BeautifulSoup


def get_html(url):
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Pragma': 'no-cache',
        'Referer': 'https://www.yougou.com/f-0-Y0A-0-1.html',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0',
        'sec-ch-ua': '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }

    response = requests.get(url, headers=headers)
    parse_html(response.text)


def parse_html(html):
    soup = BeautifulSoup(html, 'html.parser')
    proList = soup.findAll("div", class_='srchlst-wrap')
    for pro in proList:
        div = pro.find_all("div")
        url = div[0].a.attrs['href']
        img = div[0].a.img.attrs['original']
        title = div[1].span.a.get_text()
        price = div[1].p.em.i.get_text()
        original_price = div[1].p.find('del').i.get_text()
        print(title, price, original_price, img, url)


if __name__ == '__main__':
    for i in range(1):
        url = f'https://www.yougou.com/f-0-Y0A-0-1-{i}.html'
        get_html(url)
